package recover

import (
	web "geektime-go-homework/web/class2"
)

type MiddlewareBuilder struct {
	Status int
	Data   []byte
	Log    func(ctx *web.Context)
}

func (m MiddlewareBuilder) Build() web.Middleware {
	return func(next web.HandleFunc) web.HandleFunc {
		return func(ctx *web.Context) {
			defer func() {
				if err := recover(); err != nil {
					ctx.RespStatusCode = m.Status
					ctx.RespData = m.Data
				}
			}()
			next(ctx)
		}
	}
}
